package com.neusoft.crm.service.impl;

import com.neusoft.crm.mapper.SaleChanceMapper;
import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.ISaleChanceService;
import com.neusoft.crm.utils.Slf4j2Logger;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售机会服务实现类
 *
 * @author msprc
 */
@Service
public class SaleChanceServiceImpl implements ISaleChanceService {

    @Autowired
    private SaleChanceMapper saleChanceMapper;

    @Cacheable(value = "locateSaleChanceList", key = "'saleChanceList: chanceStatus:'+#saleChance.chanceStatus", unless = "#result==null")
    @Override
    public List<SaleChance> selectSaleChanceByStatus(SaleChance saleChance) {
        return saleChanceMapper.selectSaleChanceByStatus(saleChance);
    }

    @CacheEvict(value = "locateSaleChanceList", allEntries = true)
    @Override
    public int insertSaleChance(SaleChance saleChance) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = saleChanceMapper.insertSaleChance(saleChance);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("销售机会插入失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @CacheEvict(value = "locateSaleChanceList", allEntries = true)
    @Override
    public int updateSaleChance(SaleChance saleChance) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = saleChanceMapper.updateSaleChance(saleChance);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("销售机会修改失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @CacheEvict(value = "locateSaleChanceList", allEntries = true)
    @Override
    public int deleteSaleChance(SaleChance saleChance) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = saleChanceMapper.deleteSaleChance(saleChance);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("销售机会删除失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @CacheEvict(value = "locateSaleChanceList", allEntries = true)
    @Override
    public int assignSalesChance(SaleChance saleChance) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = saleChanceMapper.assignSalesChance(saleChance);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("销售机会指派失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Cacheable(value = "locateSaleChanceList", key = "'saleChanceList: chanceDueId'+#saleChance.chanceDueId", unless = "#result==null")
    @Override
    public List<SaleChance> selectAssignedSalesChance(SaleChance saleChance) {
        return saleChanceMapper.selectAssignedSalesChance(saleChance);
    }

    @Cacheable(value = "locateSaleChanceList", key = "'saleChanceList: chanceTag:'+#saleChance.chanceCustName+#saleChance.chanceTitle+#saleChance.chanceLinkman ", unless = "#result==null")
    @Override
    public List<SaleChance> selectSaleChanceByTag(SaleChance saleChance) {
        return saleChanceMapper.selectSaleChanceByTag(saleChance);
    }

    @Cacheable(value = "locateSaleChanceList", key = "'saleChanceList: chanceId'+#saleChance.chanceId", unless = "#result==null")
    @Override
    public SaleChance selectSalesChanceById(SaleChance saleChance) {
        return saleChanceMapper.selectSalesChanceById(saleChance);
    }

    @Override
    @Cacheable(value = "locateSysUser", key = "'List: zhuGuanList'", unless = "#result==null")
    public List<SysUser> selectSysUserNameBySaleChanceState(SaleChance saleChance) {
        return saleChanceMapper.selectSysUserNameBySaleChanceState(saleChance);
    }
}
